package edu.fh.salzburg.its.opsystems.newfs.implementation;

import java.util.EnumSet;
import java.util.HashMap;

import edu.fh.salzburg.its.opsystems.newfs.definition.FileAttributes;
import edu.fh.salzburg.its.opsystems.newfs.definition.NewFSDirectory;
import edu.fh.salzburg.its.opsystems.newfs.definition.NewFSElement;
import edu.fh.salzburg.its.opsystems.newfs.exception.NewFSException;

public class MBRADirectory extends NewFSDirectory {
	HashMap<String,NewFSDirEntry> dirContent;
	
	public MBRADirectory(String name) {
		super();
		this.setIdentifer("");
		this.setName(name);
		this.setAttributes(EnumSet.of(FileAttributes.DIRECTORY));
		this.setDatesNow();
		dirContent = new HashMap<String,NewFSDirEntry>();
	}

	public MBRADirectory(NewFSDirEntry file) {
		super();
		this.setDirEntry(file);
		dirContent = new HashMap<String,NewFSDirEntry>();
	}
	
	public HashMap<String, NewFSDirEntry> getDirContent() {
		return dirContent;
	}
	
	public void addElement(NewFSElement element) throws NewFSException {
		String name = element.getName();
		
		if (dirContent.containsKey(name)) {
			throw new NewFSException("Directory already contains an element with name " + name);
		}
		
		dirContent.put(name, element.getDirEntry());
		this.setSize(dirContent.size());
	}

	public void removeElement(NewFSElement element) throws NewFSException {
		String name = element.getName();
		
		if (dirContent.containsKey(name)) {
			dirContent.remove(name);
		}
		else { 
			throw new NewFSException("Directory does not contain an element with name " + name);
		}
		this.setSize(dirContent.size());
	}
	public void removeElement(NewFSDirEntry element) throws NewFSException {
		String name = element.getName();
		
		if (dirContent.containsKey(name)) {
			dirContent.remove(name);
		}
		else { 
			throw new NewFSException("Directory does not contain an element with name " + name);
		}
		this.setSize(dirContent.size());
	}
	public boolean containsElement(String name) {
		return this.dirContent.containsKey(name);
	}

	public NewFSDirEntry getElement(String name) {
		return this.dirContent.get(name);
	}
}
